<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Area;
use App\Models\BClass;
use App\Models\BOrder;
use App\Models\Chest;
use App\Models\Lock;
use App\Models\Order;
use App\Models\Remit;
use App\Models\Shop;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class CheckController extends Controller
{
    public function shop(Request $request)
    {
        $admin = Session::get('admin');
        if($request->isMethod('post')){
            if($request->has('money')){
                if($request->hasFile('pic')){
                    $remit = new Remit();
                    $remit->shopid=$request->get('shop_id');
                    $remit->time=date('Y-m-d H:i:s',time());
                    $remit->money = $request->get('money');
                    $remit->pic = env('APP_URL')."/".$request->pic->store('upload/pingzheng/');
                    $remit->save();
                    return redirect()->back()->with(['message'=>"更新成功"]);
                }else{
                    return redirect()->back()->with(['message'=>"请上传凭证"]);
                }
            }else{
                $shop = Shop::find($request->get('id'));
                $shop->status=0;
                $shop->district=$request->get('district',$shop->district);
                $shop->classify=$request->get('class_id',$shop->classify);
                $shop->pushmoney=$request->get('rate',20);
                $shop->save();
                DB::table('b_grogshopadmin')->where('grogshopid',$shop->id)->update(['status'=>0]);
                return redirect()->back()->with(['message'=>"更新成功"]);
            }
        }
        if($admin->type==1){
            if($request->has('admin_id')){
                $shop = Shop::where([['adminid',$request->get('admin_id')],['status','<>',3]])->orderBy('status','desc')->orderBy('addtime','desc')->paginate(10);
            }else{
                $shop = Shop::where('status','<>',3)->orderBy('status','desc')->orderBy('addtime','desc')->paginate(10);
            }
        }else{
            $shop = Shop::where([['adminid',$admin->admin_id],['status','<>',3]])->orderBy('status','desc')->orderBy('addtime','desc')->paginate(10);
        }
        foreach ($shop as $value){
            $sum=0;
            foreach ($value->my_chest as $v){
                $sum+= BOrder::where('chestid',$v->id)->sum('bgoods_amount');
            }
            $dakuan = Remit::where('shopid',$value->id)->sum('money');
            $value->sell=$sum;
            $value->dakuan=$dakuan;
        }
        $area = Area::where('fatherID',510100)->get();
        $class = BClass::all();
        return view('admin.shop_list',['shop'=>$shop,'area'=>$area,'admin_type'=>$admin->type,'class'=>$class]);
    }

    public function shop_del(Request $request)
    {

        DB::table('b_grogshopadmin')->where('grogshopid',$request->get('shop_id'))->update(['status'=>1,'token'=>null]);
        $shop = Shop::find(($request->get('shop_id')));
        if($shop->status==0){
            $shop->status=1;
        }
        if($shop->status==1){
            $shop->status=3;
        }
        $shop->save();
        return redirect()->back()->with(['message'=>"删除成功"]);

    }
    public function chest($shop_id,Request $request)
    {
        if($request->isMethod('post')){
            $chest = new Chest();
            $code=rand(100000,999999);
            while (Chest::where('code',$code)->first()){
                $code=rand(100000,999999);
            }
            $chest->shopid=$request->get('shop_id');
            $chest->code=$code;
            $chest->name="新柜机";
            $chest->save();
            for ($i=1;$i<=24;$i++){
                $lock = new Lock();
                $lock->chestid=$chest->id;
                $lock->locknum = $i;
                $lock->save();
            }
        return redirect()->back()->with(['message'=>"添加成功"]);
        }
        $check = Chest::where('shopid',$shop_id)->get();
        return view('admin.check_list',['check'=>$check,'shop_id'=>$shop_id]);
    }

    public function businessman(Request $request)
    {
        if($request->isMethod('post')){
            if($request->has('username')){
                if(Admin::where('username',$request->get('username'))->first()){
                    return redirect()->back()->with(['message'=>"用户名重复了"]);
                }
                $admin = new Admin();
                $admin->username=$request->get('username');
                $admin->password=$request->get('password');
                $admin->type=2;
                $admin->save();
                DB::table('b_distributorcode')->insert([
                    'adminid'=>$admin->admin_id,
                    'distributorcode'=>str_random(6),
                ]);
                return redirect()->back()->with(['message'=>"添加成功"]);
            }
            if($request->has('admin_id')){
                $admin = Admin::find($request->get('admin_id'));
                $admin->password = $request->get('password');
                $admin->save();
                return redirect()->back()->with(['message'=>"修改成功"]);
            }
        }

        $admin = Admin::where('type',"<>",3)->get();
        return view('admin.businessman',['admin'=>$admin]);
    }

    public function businessman_del(Request $request)
    {
        $shop = DB::table('b_shop')->where([['adminid',$request->get('admin_id')],['status',"=",0]])->first();

        if(!$shop){
            Admin::where('admin_id',$request->get('admin_id'))->update(['type'=>3]);

           return redirect()->back()->with(['message'=>"删除成功"]);

        }else{
            return redirect()->back()->with(['message'=>"删除失败，有正常营业店铺"]);
        }
    }
}
